package com.mgfireworks.mgplatform;

import java.util.ArrayList;
import java.util.List;

import org.rythmengine.spring.web.RythmViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;

import cn.hn.java.summer.mvc.JsonViewResolver;

@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter{ 
	
	/**
	 * 网站根目录
	 */
	public static String webRootDir;
	
    /**
     * 配置拦截器
     * @author lance
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(createActionInterceptor()).addPathPatterns("/**");
    }
    
    /**
     * 表态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

            registry.addResourceHandler("/static/**")
                    .addResourceLocations("/static/");
    }
    
    @Bean
    public ActionInterceptor createActionInterceptor(){
    	return new ActionInterceptor();
    }
    
    @Override
    public void configureContentNegotiation(
            ContentNegotiationConfigurer configurer) {
        configurer.ignoreAcceptHeader(true).defaultContentType(
                MediaType.TEXT_HTML);
    }
    
    /**
     * 初始化视图解析器
     * @param manager
     * @return
     */
    @Bean
    public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager){

    	List<ViewResolver> resolvers = new ArrayList<ViewResolver>();

    	//页面视图解析器
    	resolvers.add(rythmViewResolver());
    	//json视图解析器
    	resolvers.add(jsonViewResolver());

    	ContentNegotiatingViewResolver viewResolver=new ContentNegotiatingViewResolver();
    	viewResolver.setViewResolvers(resolvers);
    	viewResolver.setContentNegotiationManager(manager);
    	return viewResolver;
    }
    
    @Bean
    public ViewResolver rythmViewResolver(){
    	//设置页面视图解析器
    	RythmViewResolver razorResolver=new RythmViewResolver();
    	//视图模板存放目录
    	razorResolver.setPrefix("/");
    	razorResolver.setSuffix(".html");
    	razorResolver.setContentType("text/html;charset=UTF-8");
    	razorResolver.setExposeContextBeansAsAttributes(true);
    	razorResolver.setExposePathVariables(true);
    	razorResolver.setExposeRequestAttributes(true);
    	razorResolver.setExposeSessionAttributes(true);
    	razorResolver.setExposeSpringMacroHelpers(true);
    	return razorResolver;
    }
    
    @Bean
    public ViewResolver jsonViewResolver() {
        return new JsonViewResolver();
    }
    
}